//
//  VCOfTrip.m
//  project-A (pianke)
//
//  Created by dllo on 16/2/29.
//  Copyright © 2016年 scott. All rights reserved.
//

#import "VCOfTrip.h"
#import "CellOfDidianTrip.h"
#import "ModelOfDidianTrip.h"
#import <AFNetworking.h>
#import "DestinOfInfoViewController.h"
#import <DKNightVersion.h>


@interface VCOfTrip ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)UITableView *tabview;

@property (nonatomic,retain)NSMutableArray *array;

@end

@implementation VCOfTrip

-(void)dealloc{
    
    [_strURL release];
    [_tabview release];
    [_array release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self handleData];
    [self creatTabview];
    
}

-(void)handleData{
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    NSURL *URL = [NSURL URLWithString:self.strURL];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error);
        } else {
            self.array = [NSMutableArray array];
            for (NSDictionary *dic in responseObject) {
                ModelOfDidianTrip *model = [[ModelOfDidianTrip alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                model.description_title = [dic objectForKey:@"description"];
                [self.array addObject:model];
                [model release];
            }
            [self.tabview reloadData];
        }
    }];
    [dataTask resume];

    
}

-(void)creatTabview{
    self.tabview = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tabview];
    self.tabview.delegate =self;
    self.tabview.dataSource = self;
    [self.tabview registerClass:[CellOfDidianTrip class]forCellReuseIdentifier:@"CellOfDidianTrip"];
    
    self.tabview.dk_backgroundColorPicker  = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.64 green:0.74 blue:0.86 alpha:1]);
    
    [self.tabview release];

    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CellOfDidianTrip *cell = [tableView dequeueReusableCellWithIdentifier:@"CellOfDidianTrip"];
    ModelOfDidianTrip *model = [self.array objectAtIndex:indexPath.row];
    [cell passModel:model];
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ModelOfDidianTrip *model =[self.array objectAtIndex:indexPath.row];
    DestinOfInfoViewController *vc = [[DestinOfInfoViewController alloc]init];
    vc.strURL = [NSString stringWithFormat:@"http://chanyouji.com/api/plans/%@.json",model.id];
    [self.navigationController pushViewController:vc animated:YES];

    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 260;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
